//
//  ZSY_SearchEndViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/7.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_SearchEndViewController.h"
#import "ZSY_SearchEndTableViewCell.h"
#import "ZSY_SearchUserModel.h"
#import "ZSY_SearchTripModel.h"
#import "ZSY_SearchTripTableViewCell.h"
#import "ZSY_SearchDetailViewController.h"

@interface ZSY_SearchEndViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) ZSY_BaseTableView *myTableView;

@end

@implementation ZSY_SearchEndViewController

- (void)setTripArray:(NSMutableArray *)tripArray {
    if (_tripArray != tripArray) {
        [_tripArray release];
        _tripArray = [tripArray retain];
    }
    [self.myTableView reloadData];
}

- (void)setAllArray:(NSMutableArray *)allArray {
    if (_allArray != allArray) {
        [_allArray release];
        _allArray = [allArray retain];
    }
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_new_poi_back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    self.searchField = [[ZSY_BaseTextField alloc] initWithFrame:CGRectMake(50, 5, WIDTH - 50 - 20, 34)];
    self.searchField.backgroundColor = [UIColor lightGrayColor];
    self.searchField.layer.cornerRadius = 17;
    self.searchField.layer.masksToBounds = YES;
    self.searchField.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:_searchField];
    [_searchField release];
    
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
//    self.myTableView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    
    self.myTableView.separatorStyle = NO;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    static NSString *tripidentifier = @"tripCell";
    ZSY_SearchEndTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZSY_SearchEndTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    ZSY_SearchTripTableViewCell *tripCell = [tableView dequeueReusableCellWithIdentifier:tripidentifier];
    if (!tripCell) {
        tripCell = [[ZSY_SearchTripTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tripidentifier];
    }
    
    if (indexPath.section == 0) {
        // 设置高亮搜索的文字覆盖
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:[self.userName objectAtIndex:indexPath.row]];
        [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[[self.userName objectAtIndex:indexPath.row] rangeOfString:self.searchField.text]];
        NSLog(@"%@", _searchField.text);
        ZSY_SearchUserModel *model = [_allArray objectAtIndex:indexPath.row];
        cell.users = model;
        cell.nameLabel.attributedText = att;
        return cell;
    } else {
        ZSY_SearchTripModel *trip = [_tripArray objectAtIndex:indexPath.row];
        tripCell.trips = trip;
        return tripCell;
    }
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //点击后自动取消选中置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        ZSY_SearchDetailViewController *vc = [[ZSY_SearchDetailViewController alloc] init];
        ZSY_SearchTripModel *model = [_tripArray objectAtIndex:indexPath.row];
        NSString *url = [NSString stringWithFormat:@"%@", model.id];
        vc.url = url;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _allArray.count;
    } else {
        
        return _tripArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        return 60;
    } else {
        return 110;
    }
}

- (void)leftButtonAction:(UIBarButtonItem *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
